<?php

namespace app\modules\user\forms\address;

use app\common\forms\BaseForm;
use app\utils\CoordinateUtil;
use app\utils\UserUtil;

class AddressForm extends BaseForm{

    public $user_id;
    public $address_id;
    public $name;
    public $mobile;
    public $address;
    public $hs_addr;
    public $lng_lat;

    public function scenarios()
    {
        return [
            'create'=>[
                'user_id','name','mobile','address','hs_addr','lng_lat',
            ],
            'update'=>[
                'user_id','address_id','name','mobile','address','hs_addr','lng_lat',
            ],
        ];
    }

    public function rules()
    {
        return [
            ['user_id','default','on'=>['create','update'],'value'=>UserUtil::getModuleUserIdentity("user")->getId()],
            ['address_id','required','on'=>['update'],'message'=>\Yii::t("app","ID不能为空")],
            ['name','required','on'=>['create','update'],'message'=>\Yii::t("app","姓名不能为空")],
            ['mobile','required','on'=>['create','update'],'message'=>\Yii::t("app","手机号不能为空")],
            ['address','required','on'=>['create','update'],'message'=>\Yii::t("app","详细地址不能为空")],
            ['hs_addr','required','on'=>['create','update'],'message'=>\Yii::t("app","门牌或街道地址不能为空")],
            ['lng_lat','required','on'=>['create','update'],'message'=>\Yii::t("app","经纬度不能为空")],
            ['lng_lat','checkLngLat','on'=>['create','update']],
        ];
    }

    public function checkLngLat($attribute,$params){
        if(!CoordinateUtil::checkFormat($this->$attribute)){
            $this->addError("lng_lat",\Yii::t("app","经纬度格式错误"));
        }
    }
}